export default class Utils {
  static _ins: Utils
  static get instance(): Utils {
    return this._ins || new Utils
  }
  constructor() {
    Utils._ins = this
  }

  moduleName = 'Utils'

  /**
   * @param name 获取url中参数函数
   */
  getParameterByNameMap = null
  getParameterByName(name) {
    if (!this.getParameterByNameMap) {
      this.getParameterByNameMap = {}
      var interrogationIndex = location.href.indexOf('?') + 1
      var str = interrogationIndex === 0 ? '' : location.href.slice(interrogationIndex)
      if (str) {
        var arr = str.split(/&|%26/)
        arr.forEach(item => {
          var arr = item.split(/=|%3D/)
          var key = arr[0]
          var val = arr[1]
          this.getParameterByNameMap[key] = val
        })
      }
    }
    var params: Object = this.getParameterByNameMap
    return params.hasOwnProperty(name) ? params[name] : null
  }

  /**
   * 获取随机数
   */
  get random(): number {
    return Math.random() * 1000
  }

  /**
   * 获取dom元素的相对浏览器顶部的高度
   */
  getElementTop(element): number {
    var actualTop = element.offsetTop;
    var current = element.offsetParent;
    while (current !== null) {
      actualTop += current.offsetTop;
      current = current.offsetParent;
    }
    return actualTop;
  }

}